<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>编辑总进度计划</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>总进度计划编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="hidden" id="enginid">
                            <input type="text" disabled id="enginid_name" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>进度计划名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>计划总产值(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="chanfee" id="chanfee" placeholder="请输入" disabled autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>计划开始日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="startdt" id="startdt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>计划结束日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="enddt" id="enddt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">编制人</label>
                        <div class="layui-input-block">
                            <input type="text" name="baner" id="baner" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">编制日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="dt" id="dt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <?php include '../public/upinfo.php'?>
        </div>



        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>计划明细</span></div>
                        <div>
                            <button type="button" onclick="choose_contract_shi_gong()" class="layui-btn layui-btn-sm mingxibtn layui-btn-primary">从工程量清单导入</button>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" onclick="import_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">Excel导入</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>编码</th>
                            <th><text>*</text>分部名称</th>
                            <th>分项名称</th>
                            <th><text>*</text>单位</th>
                            <th><text>*</text>单价(元)</th>
                            <th><text>*</text>总计划工程量</th>
                            <th><text>*</text>总计划产值(元)</th>
                            <th><text>*</text>总计划工期(天)</th>
                            <th>计划开工日期</th>
                            <th>计划完工日期</th>
                            <th>负责人</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>


        </div>



    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;

    $("#commentForm").validate({
        rules: {
            name: {required: true},
            startdt: {required: true},
            enddt: {required: true},
            baner: {required: true},
            dt: {required: true},
        },
        messages:{
            name: {required: "请输入"},
            startdt: {required: "请选择"},
            enddt: {required: "请选择"},
            baner: {required: "请输入"},
            dt: {required: "请选择"},
        }
    });



    function choose_contract_shi_gong(){
        var enginid = $("#enginid").val();
        openPage('../public/choose_contract_shi_gong.php?page=speed_zong&enginid='+enginid,'80%','80%');
    }

    function import_list() {
        openPage('../public/import.php?reveal=speed_zong@importdata', '770px', '490px');
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_speed_zong_edit, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                var engininfo = res.data.engininfo;

                $("#enginid").val(engininfo.id);
                $("#enginid_name").val(engininfo.name);


                $("#number").val(data.number);
                $("#name").val(data.name);
                $("#chanfee").val(data.chanfee);
                $("#startdt").val(data.startdt);
                $("#enddt").val(data.enddt);
                $("#dt").val(data.dt);
                $("#baner").val(data.baner);
                $("#explain").val(data.explain);


                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input code"></td>';
                listAddStr += '<td><input type="text" class="layui-input buname"></td>';
                listAddStr += '<td><input type="text" class="layui-input xiangname"></td>';
                listAddStr += '<td><input type="text" class="layui-input unit"></td>';
                listAddStr += '<td><input type="number" class="layui-input price"></td>';
                listAddStr += '<td><input type="number" class="layui-input num"></td>';
                listAddStr += '<td><input type="number" class="layui-input chanfee"></td>';
                listAddStr += '<td><input type="number" class="layui-input duration"></td>';
                listAddStr += '<td><input type="text" class="layui-input startdt bdate_ymd" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input enddt bdate_ymd" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input baner"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);

                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input code" value="'+list[key].code+'" listid="'+list[key].id+'"></td>';
                        str += '<td><input type="text" class="layui-input buname" value="'+list[key].buname+'"></td>';
                        str += '<td><input type="text" class="layui-input xiangname" value="'+list[key].xiangname+'"></td>';
                        str += '<td><input type="text" class="layui-input unit" value="'+list[key].unit+'"></td>';
                        str += '<td><input type="number" class="layui-input price" value="'+list[key].price+'"></td>';
                        str += '<td><input type="number" class="layui-input num" value="'+list[key].num+'"></td>';
                        str += '<td><input type="number" class="layui-input chanfee" value="'+list[key].chanfee+'"></td>';
                        str += '<td><input type="number" class="layui-input duration" value="'+list[key].duration+'"></td>';
                        str += '<td><input type="text" class="layui-input startdt bdate_ymd" value="'+list[key].startdt+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input enddt bdate_ymd" value="'+list[key].enddt+'" readonly></td>';
                        str += '<td><input type="text" class="layui-input baner" value="'+list[key].baner+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }

                update_refresh_row_func();

                public_upload_file_load('upload_files', res.data.files)
                render_date();
                render_from();
                initParseMoney();
            }
        });


        form.on('submit(add)', function(data){
            parseMoney(data);


            var validRes = $("#commentForm").valid();
            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var listid = $(this).find('.code').attr('listid') || 0;
                    var code = $(this).find('.code').val();
                    var buname = $(this).find('.buname').val();
                    var xiangname = $(this).find('.xiangname').val();
                    var unit = $(this).find('.unit').val();
                    var price = $(this).find('.price').val();
                    var num = $(this).find('.num').val();
                    var chanfee = $(this).find('.chanfee').val();
                    var duration = $(this).find('.duration').val();
                    var startdt = $(this).find('.startdt').val();
                    var enddt = $(this).find('.enddt').val();
                    var baner = $(this).find('.baner').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!buname){
                        throw new Error('第'+index+'行，请输入分部名称');
                    }
                    if(!unit){
                        throw new Error('第'+index+'行，请输入单位');
                    }
                    if(!price){
                        throw new Error('第'+index+'行，请输入单价');
                    }
                    if(!num){
                        throw new Error('第'+index+'行，请输入总计划工程量');
                    }
                    if(!chanfee){
                        throw new Error('第'+index+'行，请输入总计划产值');
                    }
                    if(!duration){
                        throw new Error('第'+index+'行，请输入总计划工期');
                    }

                    var chongKey = check_chongfu(i, code, buname, xiangname, unit, price);
                    if(chongKey){
                        throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                    }

                    list.push({
                        id:listid,
                        code:code,
                        buname:buname,
                        xiangname:xiangname,
                        unit:unit,
                        price:price,
                        num:num,
                        chanfee:chanfee,
                        duration:duration,
                        startdt:startdt,
                        enddt:enddt,
                        baner:baner,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            facade.ajax({url: ask_speed_zong_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });


    //检测重复
    function check_chongfu(index, code, buname, xiangname, unit, price){
        try {
            $('#list_list tr').each(function (i) {
                var cur_code = $(this).find('.code').val();
                var cur_buname = $(this).find('.buname').val();
                var cur_xiangname = $(this).find('.xiangname').val();
                var cur_unit = $(this).find('.unit').val();
                var cur_price = $(this).find('.price').val();
                if(index != i){
                    if(code == cur_code && buname == cur_buname && xiangname == cur_xiangname && unit == cur_unit && price == cur_price){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }

    $('#list_list').on('input propertychange','.chanfee',function () {
        jisuan_money_all();
    });

    function jisuan_money_all() {
        var allchanfee = 0;
        $('#list_list tr').each(function (i) {
            var chanfee = parseValidNum($(this).find('.chanfee').val());
            allchanfee = parseFloat(allchanfee) + parseFloat(chanfee);
        });
        console.log(allchanfee)
        $("#chanfee").val(parseFormatNum(allchanfee,2));
    }


    function update_refresh_row_func() {
        render_date();
        render_from();
        jisuan_money_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }









    var ctrlv_field_name = [
        {field: 'code', name:'编码'},
        {field: 'buname', name:'分部名称'},
        {field: 'xiangname', name:'分项名称'},
        {field: 'unit', name:'单位'},
        {field: 'price', name:'单价',type:'number'},
        {field: 'num', name:'总计划工程量',type:'number'},
        {field: 'chanfee', name:'总计划产值',type:'number'},
        {field: 'duration', name:'总计划工期',type:'number'},
        {field: 'startdt', name:'计划开工日期',type:'date'},
        {field: 'enddt', name:'计划完工日期',type:'date'},
        {field: 'baner', name:'负责人'},
        {field: 'explain', name:'备注'},
    ];
    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#list_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td><input type="text" class="layui-input code" value="'+list[key].code+'"></td>';
                str += '<td><input type="text" class="layui-input buname" value="'+list[key].buname+'"></td>';
                str += '<td><input type="text" class="layui-input xiangname" value="'+list[key].xiangname+'"></td>';
                str += '<td><input type="text" class="layui-input unit" value="'+list[key].unit+'"></td>';
                str += '<td><input type="number" class="layui-input price" value="'+list[key].price+'"></td>';
                str += '<td><input type="number" class="layui-input num" value="'+list[key].num+'"></td>';
                str += '<td><input type="number" class="layui-input chanfee" value="'+list[key].chanfee+'"></td>';
                str += '<td><input type="number" class="layui-input duration" value="'+list[key].duration+'"></td>';
                str += '<td><input type="text" class="layui-input startdt bdate_ymd" value="'+list[key].startdt+'" readonly></td>';
                str += '<td><input type="text" class="layui-input enddt bdate_ymd" value="'+list[key].enddt+'" readonly></td>';
                str += '<td><input type="text" class="layui-input baner" value="'+list[key].baner+'"></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';
                $("#list_list").append(str);
            }
        }

        update_refresh_row_func()

        render_date();
        render_from();
    }
</script>
</body>
</html>
